
#include <iostream>
#include "guisettings.h"

GUISettings::GUISettings(QObject *parent) :
    QObject(parent)
{
    values = new QHash<QString, QString>();
}

/*
GUISettings::~GUISettings()
{
    delete values;
}
*/

void GUISettings::setFile(QString path)
{
    this->path = path;
}

QHash<QString, QString> *GUISettings::read()
{
    QFile file(this->path);
    if(!file.open(QIODevice::ReadOnly)) {
        QMessageBox::information(0, "error", file.errorString());
    }

    QTextStream in(&file);

    while(!in.atEnd()) {
        QString line = in.readLine();
        QStringList fields = line.split("=");
        values->insert(fields.at(0), fields.at(1));
    }
    file.close();
    return values;
}

void GUISettings::save(QHash<QString, QString> *values)
{
    QFile file(this->path);
    if(!file.open(QIODevice::WriteOnly)) {
        QMessageBox::information(0, "error", file.errorString());
    }

    file.remove();
    file.open(QIODevice::WriteOnly);

    QTextStream out(&file);
    QString line = "";
    QHash<QString, QString>::iterator iter;
    for (iter = values->begin(); iter != values->end(); ++iter){
        line += iter.key() + "=" + iter.value() + "\n";
        std::cout << line.toStdString() << "inserted\n";
    }
    out << line;

    file.close();
}
